import Alamofire

extension APIClient {
    
    // MARK: 登录接口
    func requestSaveAccount(params: [String: Any], getResponse:@escaping (_ result: ClientResponseType) -> Void) {
        let requestUrl = APIURL + "Account.SaveAccount"
        defaultRequest(url: requestUrl, params: params) { (result) in
            getResponse(result)
        }
    }
    
    // MARK: 获取账本归档记录
    func fetchAccountList(params: [String: Any], getResponse:@escaping (_ result: ClientResponseType) -> Void) {
        let requestUrl = APIURL + "Account.GetAccountList"
        defaultRequest(url: requestUrl, params: params) { (result) in
            var response = result
            switch result {
            case let .success(info, message):
                let json = info as! JSON
                var accountList = [AccountModel]()
                if let accountJsonList = json["accountList"].array {
                    for accountJson in accountJsonList {
                        let account = AccountModel(json: accountJson)
                        accountList.append(account)
                    }
                }
                let data = accountList
                response = .success(data as AnyObject, message)
            default: break
            }
            getResponse(response)
        }
    }
    
    // MARK: 删除已经归档的账本数据
    func requestDeleteAccount(params: [String: Any], getResponse:@escaping (_ result: ClientResponseType) -> Void) {
        let requestUrl = APIURL + "Account.DeleteAccount"
        defaultRequest(url: requestUrl, params: params) { (result) in
            getResponse(result)
        }
    }
    
    
    // MARK: 获取最新归档账本数据
    func fetchLastAccount(params: [String: Any], getResponse:@escaping (_ result: ClientResponseType) -> Void) {
        let requestUrl = APIURL + "Account.GetLastAccount"
        defaultRequest(url: requestUrl, params: params) { (result) in
            var response = result
            switch result {
            case let .success(info, message):
                let json = info as! JSON
                let account = AccountModel(json: json)
                response = .success(account as AnyObject, message)
            default: break
            }
            getResponse(response)
        }
    }


}
